package com.ms.qw.server.infrastructure.persistence.util;

import java.util.Collection;
import java.util.Iterator;

/**
 * User: Kaiser
 * Date: Oct 21, 2007
 */
public class InClauseConstructor {

    /**
     * The elements of the collection must be of type InClauseSource.
     *
     * @param collection
     * @return A string that could be used in "IN" clause. Such as: "((a,b),(c,d))"
     */




    /**
     * The elements of the collection must be of type InClauseSource.
     *
     * @param collection
     * @return A string that could be used in "IN" clause. Such as: "((a,b),(c,d))"
     */
    public static String getInClause(Collection collection, ValueProvider valueProvider) {
        if (collection == null || collection.size() == 0) return "";
        StringBuffer sb = new StringBuffer();
        sb.append("(");
        for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
            Object obj = iterator.next();
            sb.append(valueProvider.getValue(obj));
            sb.append(",");
        }
        sb.replace(sb.length() - 1, sb.length(), ")");
        return sb.toString();
    }
    public static String getInClauseValues(Collection collection, ValueProvider valueProvider) {
        if (collection == null || collection.size() == 0) return "";
        StringBuffer sb = new StringBuffer();
        sb.append("( VALUES ");
        for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
            Object obj = iterator.next();
            sb.append(valueProvider.getValue(obj));
            sb.append(",");
        }
        sb.replace(sb.length() - 1, sb.length(), ")");
        return sb.toString();
    }

}


